package com.ld.security.common.mybatis.base.vo;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ld.security.common.core.util.ReflectionUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Data
public class PageVo<E> extends ListVo<E> {


	@ApiModelProperty("总数")
	private long total;



	public PageVo uniquely(String fieldName){
		Map map = new HashMap<>();
		List<E> list = this.getList();
		for (E e : list) {
			Object key =  ReflectionUtil.invokeGetter(e,fieldName);
			map.put(key,e);
		}
		list.stream().filter(v -> {
			String key = ReflectionUtil.invokeGetter(v,fieldName);
			return map.get(key) != null;
		}).collect(Collectors.toList());
		return this;
	}


	public PageVo deleteNull(){

		List<E> list  = 	new LinkedList<>();

		for (E e : this.getList()) {
			if(e != null){
				list.add(e);

			}else{
				this.setTotal(this.getTotal() - 1);
			}
		}
		this.setList(list);
		return this;
	}

	public static <E> PageVo<E> of(List<E> List){
		PageVo<E> vo = new PageVo<>();
		vo.setList(List);
		vo.setTotal(List.size());
		return vo;
	}


	public static <E> PageVo<E> of(Page<E> page){
		PageVo vo = new PageVo<>();
		vo.setTotal(page.getTotal());
		vo.setList(page.getRecords());
		return vo;
	}
}
